package com.cre_drt;

import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.att.intern.webservice.Webservice;

public class LogInScreen extends FragmentActivity {

	EditText username;
	EditText password;
	ProgressDialog progressDialog;
		String[] resultString;
	String usernameString;
	String passwordString;

	Webservice loginWebservice;
	SessionManager session;
	InputMethodManager imm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_screen);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Damage Assessment Reporting");

		loginWebservice = new Webservice(getApplicationContext());

		username = (EditText) findViewById(R.id.usernameTextField);
		password = (EditText) findViewById(R.id.passwordTextField);

		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage("Logging in...");
		progressDialog.setCancelable(false);

		session = new SessionManager(getApplicationContext());
		
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

		if (getIntent().getBooleanExtra("hard logout", false)) {
			DialogBox errorWindow = new DialogBox("You have been logged out due to inactivity.", false, getApplicationContext());
			errorWindow.show(getSupportFragmentManager(), "ErrorFragment");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void startWebservice(View view) {
		usernameString = username.getText().toString();
		passwordString = password.getText().toString();

		if ((usernameString.equals("")) || (passwordString.equals(""))) {
			DialogBox errorWindow = new DialogBox("Please enter a valid username and password.", false, getApplicationContext());
			errorWindow.show(getSupportFragmentManager(), "ErrorFragment");
			return;
		}
		
		imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(password.getWindowToken(), 0);

		// Display spinning wheel and run task in background
		progressDialog.show();
		
		new loginAsyncTaskClass().execute();
	}

	private class loginAsyncTaskClass extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			resultString = loginWebservice.login(usernameString, passwordString);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			progressDialog.dismiss();
			login();
		}
	}

	public void login() {
		if ((resultString[0] != null) && (resultString[1] != null) && (resultString[2].equals("rcFailure") == false)) {

			String sessionTokenID = resultString[0];
			String authorizationType = resultString[1];

			String[] sessionVariable = new String[2];
			sessionVariable[0] = usernameString;
			sessionVariable[1] = authorizationType;

			// Clear password field
			password.setText("");

			// Store login info in SharedPreferences here
			session.createLoginSession(usernameString, authorizationType, sessionTokenID);

			if (authorizationType.equals("ADM")) {
				Intent loginIntentAdmin = new Intent(this, TaskSelectionScreen.class);
				startActivity(loginIntentAdmin);
			} else if (authorizationType.equals("RPT")) {
				Intent loginIntentRPT = new Intent(this, ReportSelectionScreen.class);
				startActivity(loginIntentRPT);
			}
		}

		else {
			DialogBox errorWindow = new DialogBox("Username or password were incorrect. Please re-enter them to login.", false, getApplicationContext());
			errorWindow.show(getSupportFragmentManager(), "ErrorFragment");
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		session.softLogout();
	}
	
	public void easterEgg(View view) {
		Intent easterEggIntent = new Intent(this, EasterEggActivity.class);
		startActivity(easterEggIntent);
	}

}
